<?php
// +----------------------------------------------------------------------
// | ThinkPHP5.0 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 郉仲生 <540426628@QQ.com>
// +----------------------------------------------------------------------
// | Date：2017年7月03日
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\controller\Common;
class User extends Common
{
    public function lst()
    {
    	$userRes=db('user')->field('id,uname,sex,username,ide,birthday,email,regtime,phone')->paginate(10);
    	$this->assign([
    		'userRes'=>$userRes,
    		]);
        return view('lst');
    }


    public function add()
    {
    	//dump($_POST);die;
    	if(request()->isPost())
    	{
    		$date=db('user')->insert(input('post.'));
    		//dump($add);die;
    		//$add['password']=md5($add['password']);
    		if($date)
    		{
    			$this->success('添加用户成功！',url('lst'));
    		}else{
    			$this->error('添加用户失败！');
    		}
    		return;


    	}
    	return view();
    }

    
    public function edit($id)
    {
    	if(request()->isPost())
    	{
    		$data=input('post.');
    		//验证
    		$validate=validate('user');
    		if (!$validate->scene('edit')->check($data)) 
    		{
			    $this->error($validate->getError());
			}
    		if($data['password'])
    		{
    			$data['password']=md5($data['password']);
    		}else{
    			unset($data['password']);
    		}
    		$save=db('user')->update($data);
    		if($save!==false){
    			$this->success('修改用户成功！','lst');
    		}else{
    			$this->error('修改用户失败！');
    		}
    		return;
    	}
    	$admins=db('user')->find($id);
    	$this->assign([
    		'admins'=>$admins,
    		]);
    	return view();
    }

    public function del($id)
    {
    	if($id!=1)
    	{
    		$del=db('user')->delete($id);
    		if($del)
    		{
    			$this->success('删除用户成功！','lst');
    		}else{
    			$this->error('删除用户失败！');
    		}
    	}else{
    		$this->error('初始化用户不允许删除！');
    	}
    }


}
